//
//  WSNewsHeader.m
//  WisdomStar
//
//  Created by 谭飞 on 2018/10/23.
//  Copyright © 2018年 zhangliwen. All rights reserved.
//

#import "WSNewsHeader.h"
#import "WSBannerView.h"

#import "WSNewsInfo.h"

@interface WSNewsHeader ()

// 广告视图
@property (weak, nonatomic) IBOutlet WSBannerView *topBanner;

@property (weak, nonatomic) IBOutlet UIView *contentView;

// 资讯图片容器视图
@property (weak, nonatomic) IBOutlet UIView *imageContainer;

// 资讯标题
@property (weak, nonatomic) IBOutlet UILabel *titleLab;

// 资讯内容
@property (weak, nonatomic) IBOutlet UILabel *contentLab;

// 浏览数量
@property (weak, nonatomic) IBOutlet UILabel *viewCountLab;

// 日期
@property (weak, nonatomic) IBOutlet UILabel *timeLab;

@property (strong, nonatomic) UIStackView *stackView;


@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageViewHeight;



@end

@implementation WSNewsHeader

- (void)awakeFromNib {
    [super awakeFromNib];
 
    [self setup];
}

- (void)setup {
    // add tap gesture
    WEAKSELF
    [self addGestureWithGestureType:WJ_TapGesture completeHandler:^{
        if (weakSelf.didTapViewCallback) {
            weakSelf.didTapViewCallback();
        }
    }];
}

// add subviews
- (void)addStackView:(NSArray<__kindof UIView *> *)subviews {
    [self.stackView removeFromSuperview]; // 移除之前的视图
    self.stackView = [[UIStackView alloc] initWithArrangedSubviews:subviews];
    self.stackView.axis = UILayoutConstraintAxisHorizontal;
    self.stackView.distribution = UIStackViewDistributionFillEqually;
    self.stackView.spacing = 10.f;
    self.stackView.alignment = UIStackViewAlignmentFill;
    [self.imageContainer addSubview:self.stackView];
    [self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.imageContainer);
    }];
}

// 计算header高度
- (CGFloat)height {
    return self.contentView.xmg_bottom + 10.f;
}

#pragma mark - Setters

- (void)setBanners:(NSArray *)banners {
    self.topBanner.datas = banners;
}

- (void)setNewsInfo:(WSNewsInfo *)newsInfo {
    _newsInfo = newsInfo;
    self.titleLab.text = newsInfo.title;
    NSString *str = [NSString stringWithFormat:@"【%@】%@",newsInfo.name, newsInfo.origin];
    [self.contentLab addAttributeText:str attrs:@{NSForegroundColorAttributeName: kThemeGreenColor,
                                                                   NSFontAttributeName: WSFontMake(14)
                                                                   } range:NSMakeRange(0, newsInfo.name.length+2)];
    
    self.viewCountLab.text = [NSString stringWithFormat:@"(%@)",@(newsInfo.view)];
    self.timeLab.text = newsInfo.create_at;
    self.imageViewHeight.constant = newsInfo.top_images.count ? 92.f : 0;
    [self layoutIfNeeded];
    
    NSMutableArray *subViews = [NSMutableArray arrayWithCapacity:newsInfo.top_images.count];
    [newsInfo.top_images enumerateObjectsUsingBlock:^(ImageInfo *imageinfo, NSUInteger idx, BOOL * _Nonnull stop) {
        UIImageView *imageView = [[UIImageView alloc] init];
        [imageView imageWithURL:imageinfo.ad_url defaultImage:kPlaceHolderImage];
        [subViews addObject:imageView];
    }];

    [self addStackView:subViews];
}

@end
